clear all 
capture log close

global data "/Users/ceweber/Dropbox/JPAM_TIV_code/original_data/"
global dta "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_files/"
global dataclean "/Users/ceweber/Dropbox/JPAM_TIV_code/dta_output/"
global output "/Users/ceweber/Dropbox/JPAM_TIV_code/output/"

set more off
cd "${dataclean}"


//////////////////////////////////////////////////////////////////
///FIND OVERALL ROUNDNESS FOR THESE MULTIPLIER CANDIDATES/////////
/////////////////////////////////////////////////////////////////

use rounding_results_weekly_expand_kendall2, clear

sum group
local locationmax = r(max)

forvalues l = 1(1)`locationmax' {
	use rounding_results_weekly_expand_kendall2 if group==`l', clear
	qui sum location
	local loc = r(mean)
	qui save weekly_expand_`l'.dta, replace
	
	use collapsed_price_trans_counts_kendall if location==`loc'
	qui save collapsed_price_trans_`l'.dta, replace
	}
 	

set more off
use rounding_results_weekly_expand_kendall2, clear
sum group
local locationmax = r(max)

forvalues l =1(1)`locationmax'{
use weekly_expand_`l', clear

qui keep best_amount_quarters best_ratio_quarters location weekly tax_mult_none tax_mult_sales_only tax_mult_excise_only tax_mult_both_add tax_mult_both_mult 
rename best_ratio_quarters multiplier
rename best_amount_quarters round_amount

sum location 
local name = r(mean)
mmerge weekly using collapsed_price_trans_`l'.dta, type(1:n) ukeep(price inventoryid usableweight inventorytype)


egen rounder_group = group(multiplier)

qui sum rounder_group
local max = r(max)
local option_tally=1
	
forvalues p = 1(1)`max'{
	
	qui sum multiplier if rounder_group==`p'
	local mult = r(mean)
			
	qui gen mult_`option_tally' = `mult'
	qui gen price0 = price*mult_`option_tally'
	qui gen round_price0 = round(price0, .25)
	qui gen check = abs(price0 - round_price0)
	qui gen round = 1 if check<.01
	qui replace round = 0 if round==.
	qui rename check dist
	
	qui gen d_cut=.
	
	//calculate discount off last known round price, make discounts count as round 
	qui gen price1 = price*mult_`option_tally'
	qui gen last_round = price1 if round==1
	sort inventoryid weekly			
	bysort inventoryid: carryforward last_round, replace //we now have the last observed round price for each ob
	qui gen discount = price1/last_round
	*qui replace discount = round(discount, .01)
		
	forvalues c = .05(.05)1{
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}	
		
	foreach c in .33 .67 {
		qui replace d_cut = 1 if abs(discount-`c')<.0025&discount<.99
		}		
				
	drop discount		

	qui gen d_cut_b4 = .
	//make intro discounts count as round 
	qui gen first_round = price1 if round==1
	gsort inventoryid -weekly			
	bysort inventoryid: carryforward first_round, replace			
	qui replace first_round = . if last_round!=.
	qui gen discount = price1/first_round
	qui replace discount = round(discount, .01)
	forvalues c = .05(.05)1{			
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99			
	}		
		foreach c in .33 .67 {
		qui replace d_cut_b4 = 1 if abs(discount-`c')<.0025&discount<.99
		}	
	drop discount last_round first_round price0	
					
	//replace round if it's a discount from a round price
	sort inventoryid weekly
	qui replace round = 1 if round==0&d_cut==1&round[_n-1]==1
	gsort inventoryid -weekly
	qui replace round = 1 if round==0&d_cut_b4==1&round[_n-1]==1
	qui replace dist = . if d_cut==1&round==1
	qui replace dist = . if d_cut_b4==1&round==1
	sort inventoryid weekly
	
	*CW comment: got rid of separate creation of vars and then renaming to speed up code:
	qui egen round_`option_tally' = mean(round), by(inventoryid) //record the roundness, same as "roundness" var
	qui egen median_dist_`option_tally' = median(dist), by(inventoryid) // record the median distance, same as "distance" var
	replace median_dist_`option_tally'=round(median_dist_`option_tally',.001)
	qui gen day_round_`option_tally' = round
			
	drop round_price0 round dist d_cut price1 d_cut_b4
	local option_tally = `option_tally' + 1
	} 


//////////////
//WHICH OPTION IS THE BEST
////////////////
di "Which option is the best"
local number_of_options = `option_tally' - 1

qui gen max_round = 0
qui gen final_mult = .
qui gen trans_flag = 0
qui gen trans_tie_flag = 0
gen best_count = 0
gen max_dist = 0
gen min_count = 0

//*CW generates for later in this section: 
//qui egen firstsaleinvdate=min(date),by(inventoryid)
//qui egen lastsaleinvdate=max(date), by(inventoryid)

* what is the max roundness?
forvalues p = 1(1)`number_of_options'{
	qui replace max_round = round_`p' if (round_`p'>max_round&round_`p'!=.)
}			


*create a dummy for ties 
forvalues r = 1(1)`number_of_options'{
	qui replace best_count = best_count + 1 if round_`r'==max_round&max_round!=0
	}	

*if no ties, pick the mult 	
forvalues p = 1(1)`number_of_options'{
	qui replace final_mult = mult_`p' if round_`p'==max_round&max_round!=0&best_count<2
	}	

*pick final multiplier if there is a tie 
*create the smallest distance
forvalues p = 1(1)`number_of_options'{
	qui replace max_dist = median_dist_`p' if median_dist_`p'>max_dist&round_`p'==max_round
	}

*create flag for if multiple ones have the same min_dist as well
*note: min_count=0 means never found one with enough of a median distance gap to break the tie and min_count=2 means found two of them. in both cases tie not broken
forvalues r = 1(1)`number_of_options'{
	qui replace min_count = min_count + 1 if (median_dist_`r'-max_dist)<=-.005 &round_`r'==max_round&max_dist!=0
	}
			
*pick the one with the smallest distance 
forvalues p = 1(1)`number_of_options'{
	qui replace final_mult = mult_`p' if (median_dist_`p'-max_dist)<=-.005&round_`p'==max_round&min_count==1 
	}

gen tie = 0
replace tie = 1 if (min_count==0&best_count>1)|	(min_count>1&best_count>1)

**if there is a min_dist tie, pick 1 over 1.25
forvalues p1 = 1(1)`number_of_options'{
	qui sum mult_`p1'
	local first = r(mean)
	forvalues p2 = 1(1)`number_of_options'{
	qui sum mult_`p2'
		local second = r(mean)
		qui replace final_mult = 1 if `first'==1 & `second'==1.25 & min_count>1&min_count<.&round_`p1'==max_round&round_`p2'==max_round
		qui replace final_mult = 1 if `first'==1.25 & `second'==1 & min_count>1&min_count<.&round_`p1'==max_round&round_`p2'==max_round
		qui replace tie = 0 if `first'==1 & `second'==1.25 & min_count>1&min_count<.&round_`p1'==max_round&round_`p2'==max_round
		qui replace tie = 0 if `first'==1.25 & `second'==1 & min_count>1&min_count<.&round_`p1'==max_round&round_`p2'==max_round
		}
		}
gen daily_round = .			
forvalues p = 1(1)`number_of_options'{
	qui replace daily_round = day_round_`p' if round_`p'==max_round&max_round!=0&best_count<2
	qui replace daily_round = day_round_`p' if (median_dist_`p'-max_dist)<=-.005&round_`p'==max_round&min_count==1
	qui replace daily_round = day_round_`p' if (median_dist_`p'-max_dist)<=-.005&round_`p'==max_round&min_count==1&max_round!=0
	}		



//////////////////////////////////////////////////////////////////
///SELECT WHICH ROUNDER EACH WEEK SHOULD HAVE/////////
/////////////////////////////////////////////////////////////////

if `max'==1 {
sum mult_1
replace final_mult = r(mean) 
}
*replace final_mult=. if tie==1


*CW changes how tally these. now mark for each multiplier what should be counted in the final tally:
forvalues p=1(1)`number_of_options'{
gen multcount_`p'=1
replace multcount_`p'=0 if mult_`p'!=final_mult&tie==0
}

*then add ties in:
forvalues p=1(1)`number_of_options'{
replace multcount_`p'=0 if tie==1&round_`p'!=max_round
}


*CW: leave in ties, so that we choose the actual dominant multipliers
*drop if tie==1


forvalues r = 1(1)`number_of_options'{
egen sum_mult`r'= sum(multcount_`r'), by(weekly)
sum mult_`r' if multcount_`r'==1
		local name = round(r(mean), .001)
		label var  sum_mult`r' "mult = `name'"
		}


/* check plots
summ location
local name = r(mean)
sort weekly
twoway (line sum_mult* weekly), ///  
xline(2886, lcolor(black) lpattern(dash)) title("Stage Two All Transactions, Inventory Level, Firm `name'") ///
ytitle("Multiplier Count", margin(medsmall))  xtitle("Week")   ///
graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
legend(region(lcolor(none))) xlabel(#6, valuelabel angle(forty_five))
graph export stagetwo_`name'.png, as(png) replace
*/

gen max_count = 0
forvalues r = 1(1)`number_of_options'{
	replace max_count = sum_mult`r' if sum_mult`r'>max_count
	}

gen multiplier_weekly = .
forvalues r = 1(1)`number_of_options'{
	sum mult_`r' if rounder_group==`r'
	local mult = r(mean)
	replace multiplier_weekly = `mult' if sum_mult`r'==max_count&sum_mult`r'!=.
	}
	bysort weekly: egen mult_mean = mean(multiplier_weekly)
	replace multiplier_weekly = mult_mean

collapse multiplier_weekly, by(weekly location)
	
	
sum location 
local name = r(mean)

save mult_`l'.dta, replace
}

